import { Body, Controller, Get, Param, Post } from '@nestjs/common';
import { BaseController } from '@/controllers/base.controller';
import { WishService } from '@/services/wish.service';
import {
  WishOrderDto,
  WishQueryDto,
  WishTransaction2Dto,
} from '@/dtos/wish.dto';
import { halt, ok } from '@/utils';
import { CurrentUser } from '@/decorators';

@Controller('wish')
export class WishController extends BaseController {
  constructor(private readonly wishService: WishService) {
    super();
  }

  @Post('order/create')
  async order(@Body() dto: WishOrderDto, @CurrentUser('id') userId: number) {
    return ok(await this.wishService.order(dto, userId));
  }

  @Get('order/cancel/:no')
  async cancel(@Param('no') no: string, @CurrentUser('id') userId: number) {
    return ok(await this.wishService.cancel(no, userId));
  }

  @Post('order/list')
  async list(@Body() dto: WishQueryDto, @CurrentUser('id') userId: number) {
    return ok(await this.wishService.list(dto, userId));
  }

  @Post('admin/query')
  async query(@Body() dto: WishQueryDto) {
    return ok(await this.wishService.query(dto));
  }

  @Post('admin/transaction')
  async transaction(@Body() dto: WishTransaction2Dto) {
    await this.redisLock('wish_transaction', dto.uid);
    try {
      return ok(await this.wishService.transaction(dto));
    } catch (e) {
      return halt(e.message);
    }
  }
}
